﻿var rowsPerPage = 10;
var totalRows = 0;
var arrUsers = [];
var arrDivInclude       = ['divFuncAction'];
var cateID = 0;

function initListCategory() {
    rowsPerPage = $("#drpNumPage").val();
    Doctors_WebForum.Admin.UIControls.Categories.GetAllCategory(function (users) {
            lstFuncs = users.value;
            arrUsers = lstFuncs;
            totalRows = dataLength = lstFuncs.length;
            if (dataLength > 0) {
                LoadFunctionOfPage(1, lstFuncs);
            } else {
            util.removeAllRows("functionGird", { filter: function (tr) { return (tr.id != "funcTemp"); } }); //xoa Grid chi trua template
            $("#trNoReSult").attr('style', 'display:');
            $("#divPaging").attr('style', 'display:none');
        }
    });
                
}
function LoadFunctionOfPage(pageCurrent, users) {
    var object = null;
    rowsPerPage = $("#drpNumPage").val();
    dataLength = users.length;
    vfrom = (pageCurrent - 1) * rowsPerPage;
    var id = 0;
    var row = 0;
    var css = '';
    util.removeAllRows("functionGird", { filter: function (tr) { return (tr.id != "funcTemp"); } });
    $("#trNoReSult").attr('style', 'display:none');
    $("#chkAll").attr("checked", 0);
    for (var i = 0; i < rowsPerPage; i++) {
        if ((vfrom + i) < dataLength) {
            object = users[vfrom + i];
            id = object.CategoryID;
            css = (i % 2 == 0) ? 'Row_Odd' : 'Row_Round';

            util.cloneNode("funcTemp", { idSuffix: id });

            $("#tdTitle" + id).html(object.Title);
            $("#tdDescription" + id).html(object.Description);
            $("#tdOrder" + id).html(object.Order);
            $("#tdStatus" + id).html(object.StrStatus);

            $("#chk" + id).attr("checked", 0);
            $("#funcTemp" + id).attr('style', 'display:');
            $("#funcTemp" + id).attr('class', css);
        }
    }
    initPaging("divPaging", dataLength, "gotoPage", 1);
}

//================================================================================================

function initPagingSearch(divPage, totalRowsSearch, functionName, currentPage) {
    $("#" + divPage).html(callPage(currentPage, 1, totalRowsSearch, rowsPerPage, functionName));
    $("#" + divPage).attr('style', 'display:');
}

//================================================================================================
//Ham phuc vu phan trang cho danh sach user
function initPaging(divPage, totalRows, functionName, currentPage) {
    $("#" + divPage).html(callPage(currentPage, 1, totalRows, rowsPerPage, functionName));
    $("#" + divPage).attr('style', 'display:');
}
function gotoPage(pageCurrent, base, functionName) {
    LoadFunctionOfPage(pageCurrent, arrUsers);
    callJumpPage("divPaging", pageCurrent, base, functionName);
}
function callJumpPage(divPage, pageCurrent, base, functionName) {
    pt = callPage(pageCurrent, base, totalRows, rowsPerPage, functionName);
    $("#" + divPage).html(pt);
    $("#" + divPage).attr('style', 'display:');
}

function DrpChange() {
    initListCategory();
}

function Function_AddNew()
{
    $("#btnAddNewAndContinue").attr('style','display:');
    $("#btnAddNewAndContinue").attr('style','width:auto');            
    _albumID=-1;
    SHObjectAll("divFuncAction");
    ShowPanel("divFuncAction");
    HiddenPanel('divChangeStatus');
    ClearFormFunction();          
    //BindStatus("drpStatus", '-1', "[ Chọn trạng thái ]");
    $('#txtTenAlbum').focus();
}

function Function_Edit(id) {
    cateID = id;
    HiddenAll('divFuncAction');
    $("#btnAddNewAndContinue").attr('style','display:none');
    Doctors_WebForum.Admin.UIControls.Categories.GetCategoryByID(cateID, function(entity){
        var ofunc = entity.value;  
        $('#hidCateID').val(ofunc.CategoryID);
        $('#txtTitle').val(ofunc.Title);              
        $('#txtOrder').val(ofunc.Order);              
        $('#txtDescription').val(ofunc.Description);
        BindStatus("drpStatus", ofunc.StrStatus, "[ Select Status ]");
    }); 
}
function Functon_Del() {
    var listIDs = getChecked("chkFunction", 3);
    if (listIDs == "") {
        alert(msgNocheck);
        return;
    }
    if (confirm("Ban co that su muon xoa cai nay?")) {
        Doctors_WebForum.Admin.UIControls.Categories.Delete(listIDs, function (bResult) {
            if (bResult.value) {
                ShowMessage("divFuncMess", "tdMessage", msgDel);
                initListUser();
            } else {
                ShowMessage("divFuncMess", "tdMessage", msgDelErr);
            }
        });
    }
}
function Function_Ban(id) {

}
function Function_Cancel()
{
    HiddenPanel("divFuncAction");
    HiddenPanel("divAnDinhAction");
}
function ClearFormFunction()
{
    alert("ClearFormFunction");
}

function Function_SaveOrUpdate(cont)
{alert(cont);
     var entity;
     if(validata()){
        if (_albumID == -1)
        {// ADD NEW...
            entity = {
                Title:$('#txtTitle').val(),
                Description: $('#txtMoTa').val(),
                Status:$('#drpStatus').val(),
                Order:$('#txtOrder').val(),
                CateID:-1
            }
        }
        else
        {//EDITING...
            entity = {
                Title:$('#txtTitle').val(),
                Description: $('#txtMoTa').val(),
                Status:$('#drpStatus').val(),
                Order:$('#txtOrder').val(),
                CateID:cateID
            }
         }
         alert(entity.Title);
         Doctors_WebForum.Admin.UIControls.Categories.SaveOrUpdate(entity, function(bResult){
            if (bResult.value){    
                initListCategory();
                ShowMessage("divFuncMess","tdMessage", msgAdd); 
                if(cont==1)
                    Functon_Add();                    
                //HiddenPanel("divFuncAction");                
            }else{                      
                ShowMessage("divFuncMess","tdMessage", msgAddErr);    
            } 
         });
     }
}

function validata()
{
    if(trim($('#txtTitle').val())=="")
    {
        alert("Vui lòng nhập title!");
        $('#txtTitle').focus();
        return false;
    }
    if(trim($('#txtDescription').val())=="")
    {
        alert("Vui lòng nhập Description!");
        $('#txtDescription').focus();
        return false;
    }
//    if($('#drpStatus').val()=='-1')
//    {
//        alert("Vui lòng chọn trạng thái!");
//        $('#drpStatus').focus();
//        return false;
//    }
    return true;
}

function BindStatus(combox, vall, arrDefault){     
    var myOption = $("#" + combox);
    var pos = true; 
    Doctors_WebForum.Admin.UIControls.Categories.StatusGetAll(function(drpcombox){
        drpCombox = drpcombox.value;
        // Clear the <select> of existing options
        myOption.empty();
        if (drpCombox.length != 0) {
            $.each(drpCombox, function() {
                if (pos) {
                    pos = false;
                    myOption.append(
                       $('<option></option').val('-1').html(arrDefault)
                    );
                    myOption.append(
                       $('<option></option').val(this.Status).html(this.StrStatus)
                    );
                }else{
                    myOption.append(
                       $('<option></option').val(this.Status).html(this.StrStatus)
                    );
               }
            });            
        }else
            myOption.append(
                       $('<option></option').val('-1').html(arrDefault)
                    );
        if (vall!=null){
            myOption.val(vall); 
        }        
    });   
}

function BindStatus1(combox){   
    var myOption = $("#" + combox);
    Doctors_WebForum.Admin.UIControls.Categories.StatusGetAll(function(drpcombox){
        drpCombox = drpcombox.value;
        // Clear the <select> of existing options
        myOption.empty();
        if (drpCombox.length != 0) {
            $.each(drpCombox, function() {
                    myOption.append(
                       $('<option></option').val(this.Status).html(this.StrStatus)
                       );
            });
        }
    });
}

function HiddenAll(divname){
    HiddenPanel('divFuncMess');
    HiddenPanel('divFuncAction');
//    if ($("#divFillter").attr('class') != "panelHidden")
//        showFilter();
    if(divname!='')
        ShowPanel(divname);
}